package com.bianmaba.oauth2.clientcredentials;

import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.UUID;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {PasswordAuthServerApplication.class})
@AutoConfigureMockMvc
public class HttpClientOauth2Tester {

    @Autowired
    private MockMvc mvc;

    @Test
    public void product() throws Exception {
        String pk = UUID.randomUUID().toString();

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/product/1");

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.is("1")));
    }

    @Test
    public void orderFailure() throws Exception {
        String pk = UUID.randomUUID().toString();

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/order/1");

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isUnauthorized());
    }


    @Test
    public void orderOK() throws Exception {

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/oauth/token");
        builder.param("username", "user_1");
        builder.param("password", "123456");
        builder.param("grant_type", "password");
        builder.param("scope", "select");
        builder.param("client_id", "client_1");
        builder.param("client_secret", "123456");
        ResultActions resultActions = this.mvc.perform(builder);
        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());

        String result = resultActions.andReturn().getResponse().getContentAsString();
        JSONTokener jsonTokener = new JSONTokener(result);
        JSONObject val = (JSONObject) jsonTokener.nextValue();
        String accessToken = val.getString("access_token");

        builder = MockMvcRequestBuilders.get("/order/1?access_token=" + accessToken);

        resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.is("1")));
    }
}
